package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.SysDepartRoleRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysDepartRoleDTO;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysDepartRoleUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门角色
 */
@Slf4j
@Api(tags = "部门角色")
@RestController
@RequestMapping("/sys/sysDepartRole")
public class SysDepartRoleController {

    @Autowired
    private SysDepartRoleRestAPI sysDepartRoleRestAPI;

    /**
     * 分页列表查询
     *
     * @param sysDepartRoleDTO
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "部门角色-分页列表查询", notes = "部门角色-分页列表查询")
    @GetMapping(value = "/list")
    public MatApiResponse<?> queryPageList(SysDepartRoleDTO sysDepartRoleDTO,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(name = "deptId", required = false) String deptId) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.queryPageList(sysDepartRoleDTO, pageNo, pageSize, deptId);
        return matApiResponse;
    }

    /**
     * 添加
     *
     * @param sysDepartRoleDTO
     * @return
     */
    //@RequiresPermissions("system:depart:role:add")
    @ApiOperation(value = "部门角色-添加", notes = "部门角色-添加")
    @PostMapping(value = "/add")
    public MatApiResponse<?> add(@RequestBody SysDepartRoleDTO sysDepartRoleDTO) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.add(sysDepartRoleDTO);
        return matApiResponse;
    }

    /**
     * 编辑
     *
     * @param sysDepartRoleDTO
     * @return
     */
    @ApiOperation(value = "部门角色-编辑", notes = "部门角色-编辑")
    //@RequiresPermissions("system:depart:role:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<?> edit(@RequestBody SysDepartRoleDTO sysDepartRoleDTO) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.edit(sysDepartRoleDTO);
        return matApiResponse;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "部门角色-通过id删除", notes = "部门角色-通过id删除")
    //@RequiresPermissions("system:depart:role:delete")
    @DeleteMapping(value = "/delete")
    public MatApiResponse<?> delete(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.delete(id);
        return matApiResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "部门角色-批量删除", notes = "部门角色-批量删除")
    //@RequiresPermissions("system:depart:role:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public MatApiResponse<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.deleteBatch(ids);
        return matApiResponse;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "部门角色-通过id查询", notes = "部门角色-通过id查询")
    @GetMapping(value = "/queryById")
    public MatApiResponse<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.queryById(id);
        return matApiResponse;
    }

    /**
     * 获取部门下角色
     *
     * @param departId
     * @return
     */
    @RequestMapping(value = "/getDeptRoleList", method = RequestMethod.GET)
    public MatApiResponse<List<SysDepartRoleDTO>> getDeptRoleList(@RequestParam(value = "departId") String departId, @RequestParam(value = "userId") String userId) {
        MatApiResponse<List<SysDepartRoleDTO>> matApiResponse = sysDepartRoleRestAPI.getDeptRoleList(departId, userId);
        return matApiResponse;
    }

    /**
     * 设置
     *
     * @param json
     * @return
     */
    //@RequiresPermissions("system:depart:role:userAdd")
    @RequestMapping(value = "/deptRoleUserAdd", method = RequestMethod.POST)
    public MatApiResponse<?> deptRoleAdd(@RequestBody JSONObject json) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.deptRoleAdd(json);
        return matApiResponse;
    }

    /**
     * 根据用户id获取已设置部门角色
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getDeptRoleByUserId", method = RequestMethod.GET)
    public MatApiResponse<List<SysDepartRoleUserDTO>> getDeptRoleByUserId(@RequestParam(value = "userId") String userId, @RequestParam(value = "departId") String departId) {
        MatApiResponse<List<SysDepartRoleUserDTO>> matApiResponse = sysDepartRoleRestAPI.getDeptRoleByUserId(userId, departId);
        return matApiResponse;
    }

    /**
     * 查询数据规则数据
     */
    @GetMapping(value = "/datarule/{permissionId}/{departId}/{roleId}")
    public MatApiResponse<?> loadDatarule(@PathVariable("permissionId") String permissionId, @PathVariable("departId") String departId, @PathVariable("roleId") String roleId) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.loadDatarule(permissionId, departId, roleId);
        return matApiResponse;
    }

    /**
     * 保存数据规则至角色菜单关联表
     */
    @PostMapping(value = "/datarule")
    public MatApiResponse<?> saveDatarule(@RequestBody JSONObject jsonObject) {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.saveDatarule(jsonObject);
        return matApiResponse;
    }

    /**
     * 导出excel
     *
     * @param sysDepartRoleDTO
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(SysDepartRoleDTO sysDepartRoleDTO) {
        ModelAndView modelAndView = sysDepartRoleRestAPI.exportXls(sysDepartRoleDTO);
        return modelAndView;
    }

    /**
     * 通过excel导入数据
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() {
        MatApiResponse<?> matApiResponse = sysDepartRoleRestAPI.importExcel();
        return matApiResponse;
    }

}
